import sys
import myUtils as ut

def printHelp():
    str = "----------------------------------------------------------------\n"
    str += "getResults v0.1\n"
    str += "---------------------------------------------------------------\n"
    str += "usage: python getResulst.py [Experiment.log] [destination.html]\n"
    str += "----------------------------------------------------------------\n"
    print(str)

def calcMean(lstLine,sex,emo,pos):
    mean = sum = count = 0
    for line in lstLine:
        col=line.split("\t")
        if(col[1] == sex and col[2] == emo and col[3] == pos):
            #print(line)
            sum += int(col[5])
            count += 1
    if(count>0):
        mean = sum/count   
    return (mean)

def calcReactions(lstLine,sex,emo,pos,label):
    count = 0
    for line in lstLine:
        col = line.split("\t")
        if(col[1] == sex and col[2] == emo and col[3] == pos and col[4] == label):
            count += 1
    return count
    
def createHtmlOutput(lstLine):
    strHtml = "<!DOCTYPE html>\n"
    strHtml += "<html><body><table border=1 cellspacing=0 cellpadding=10>\n"
    strHtml += "<tr>\n"
    strHtml += "<td>Sex</td>\n"
    strHtml += "<td>Gesichtsausdruck</td>\n"
    strHtml += "<td>Position</td>\n"
    strHtml += "<td>HA</td>\n"
    strHtml += "<td>NE</td>\n"
    strHtml += "<td>SA</td>\n"
    strHtml += "<td>GR</td>\n"
    strHtml += "<td>MW</td>\n"
    strHtml += "</tr>\n"
    strHtml += createTableLine(lstLine,"F","HA","l")
    strHtml += createTableLine(lstLine,"F","HA","m")
    strHtml += createTableLine(lstLine,"F","HA","r")
    strHtml += createTableLine(lstLine,"F","NE","l")
    strHtml += createTableLine(lstLine,"F","NE","m")
    strHtml += createTableLine(lstLine,"F","NE","r")
    strHtml += createTableLine(lstLine,"F","SA","l")
    strHtml += createTableLine(lstLine,"F","SA","m")
    strHtml += createTableLine(lstLine,"F","SA","r")
    strHtml += createTableLine(lstLine,"F","GR","l")
    strHtml += createTableLine(lstLine,"F","GR","m")
    strHtml += createTableLine(lstLine,"F","GR","r")
    strHtml += createTableLine(lstLine,"M","HA","l")
    strHtml += createTableLine(lstLine,"M","HA","m")
    strHtml += createTableLine(lstLine,"M","HA","r")
    strHtml += createTableLine(lstLine,"M","NE","l")
    strHtml += createTableLine(lstLine,"M","NE","m")
    strHtml += createTableLine(lstLine,"M","NE","r")
    strHtml += createTableLine(lstLine,"M","SA","l")
    strHtml += createTableLine(lstLine,"M","SA","m")
    strHtml += createTableLine(lstLine,"M","SA","r")
    strHtml += createTableLine(lstLine,"M","GR","l")
    strHtml += createTableLine(lstLine,"M","GR","m")
    strHtml += createTableLine(lstLine,"M","GR","r")
    strHtml += "</table>\n"
    strHtml += "</body>\n"
    strHtml += "</html>\n"
    return strHtml

def createTableLine(lstLine,sex,emo,pos):
    strHtml = ""
    strHtml += "<tr>\n"
    strHtml += "<td>"+sex+"</td>\n"
    strHtml += "<td>"+emo+"</td>\n"
    strHtml += "<td>"+pos+"</td>\n"
    strHtml += "<td>"+str(calcReactions(lstLine,sex,emo,pos,"HA"))+"</td>\n"
    strHtml += "<td>"+str(calcReactions(lstLine,sex,emo,pos,"NE"))+"</td>\n"
    strHtml += "<td>"+str(calcReactions(lstLine,sex,emo,pos,"SA"))+"</td>\n"
    strHtml += "<td>"+str(calcReactions(lstLine,sex,emo,pos,"GR"))+"</td>\n"
    strHtml += "<td>"+str(calcMean(lstLine,sex,emo,pos))+"</td>\n"
    strHtml += "</tr>\n"    
    return strHtml
    
def main():    
    if(len(sys.argv) != 3):
        print("Error: Es muessen genau zwei Parameter uebergeben werden!\n")
        sys.exit()
    # check args
    if not (ut.fileExists(sys.argv[1])):
        print("Error: Die zu ladende Experimentdatei existiert nicht!")
    strLog = sys.argv[1]
    strDest = sys.argv[2]
    lstLine = ut.loadFile(strLog)
    lstLine = lstLine[2:]
    fDest = ut.createFile(strDest)
    strHtml = createHtmlOutput(lstLine)
    fDest.write(strHtml)
    #mean = calcMean(lstLine,"M","HA","l")
    #count = calcReactions(lstLine,"M","HA","m","SA")
    #print count
    
    #print(mean)
    #for line in lstLine:
        #print(line)
    #    col=line.split("\t")
        
    
if __name__ == "__main__":
    main()